package com.yinhe.music.yhmusic.login.password;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.application.MusicApplication;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.model.LoginInfo;
import com.yinhe.music.yhmusic.personal.PersonActivity;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.ToastUtils;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;

/**
 * @author 何晓华
 */
public class PasswordActivity extends BaseServiceActivity implements PasswordContract.IPassWordView {
    private EditText pwd1;
    private EditText pwd2;
    private String mCode;
    private String number;
    private int type;

    PassWordPresenter mPresenter;

    private void setToolbar() {
        setToolbar(type == 1 ? "设置密码" : "重设密码");
    }

    @Override
    public void initView() {
        RxBus.get().register(this);

        pwd1 = findViewById(R.id.reset_password);
        pwd2 = findViewById(R.id.reset_password2);
        pwd1.setFocusable(true);
        pwd1.requestFocus();
        Button next = findViewById(R.id.next);
        next.setText("完成");

        if (getIntent() != null) {
            number = getIntent().getStringExtra("phone");
            mCode  = getIntent().getStringExtra("code");
            type   = getIntent().getIntExtra("type", 0);
        }

        setToolbar();

        next.setOnClickListener(view -> setPassword());
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_password;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new PassWordPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }


    private void setPassword() {
        String mPwd1 = pwd1.getText().toString().trim();
        String mPwd2 = pwd2.getText().toString().trim();
        if (TextUtils.isEmpty(mPwd1)) {
            ToastUtils.show("密码不能为空");
            return;
        }
        if (!TextUtils.equals(mPwd1, mPwd2)) {
            ToastUtils.show("两次输入密码不相同");
            return;
        }
        mPresenter.setPassWord(type == 1
                ? IConstants.REGISTER : IConstants.PASSWORD, number, mCode, mPwd1);
    }

    @Override
    public void setPassWordUI(LoginInfo loginInfo) {
        if (type == 1) {
            UmengEventUtils.register(this);
            Toast.makeText(PasswordActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(PasswordActivity.this, "重设密码成功", Toast.LENGTH_SHORT).show();
        }
        DataBaseAccessor.getInstance().uninit();
        DataBaseAccessor.getInstance().initDBHelper(MusicApplication.getInstance(), loginInfo.getUid() + ".db");

        Preferences.setLoginSN(loginInfo.getSn());
        RxBus.get().post(RxBusEventType.User.REGISTER_SUCCESS, loginInfo);

        Intent intent = new Intent(PasswordActivity.this, PersonActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
